package com.xingkeduo.business.operation.controller;

import com.alibaba.fastjson.JSON;
import com.xingkeduo.business.operation.domain.ShopsDomain;
import com.xingkeduo.pojo.dto.Shops.ShopsDTO;
import com.xingkeduo.pojo.dto.Shops.ShopsDetailLogDTO;
import com.xingkeduo.pojo.dto.Shops.ShopsResponseDTO;
import com.xingkeduo.pojo.json.ShopsJson;
import com.xingkeduo.pojo.query.ShopsQuery;
import com.xingkeduo.pojo.table.ShopsTable;
import com.xingkeduo.tool.ResponseUtils;
import com.xingkeduo.util.pojo.exception.SystemException;
import com.xingkeduo.util.tool.ReturnUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.xingkeduo.tool.OperaConstants.CMS_URL_PREFIX;

/**
 * 门店管理
 *
 * @author wangxilong <wangxilong@xingkeduo.com>
 * @version id:v0.1 ${date}${time}
 * @package BeautyOS
 * @copyright Copyright (c) XKD 2019
 */
@Slf4j
@RestController
@RequestMapping(CMS_URL_PREFIX + "/shops")
public class ShopsController extends InitController {

    @Autowired
    private ShopsDomain shopsDomain;

    //查看
    @RequestMapping(value = "/{urlParam:.+}", method = RequestMethod.GET)
    public ResponseUtils doGet(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            ShopsQuery shopsQuery = ShopsJson.createQueryForShops(urlParam);
            long id = shopsQuery.getId();
            ShopsDetailLogDTO shopsDetailLogDTO = shopsDomain.getShopsById(id);
            return ResponseUtils.getSuccess(shopsDetailLogDTO);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return ResponseUtils.error(throwable.getMessage(), SystemException.Type.DEFAULT.toString());
        }
    }

    /**
     * d店铺短信息
     * @param id
     * @return id name 服务详情
     */
    @GetMapping(value = "/getShopsSortInfo")
    public ResponseUtils doShopsSortInfo(Long id) {
        returnUtil = new ReturnUtil();
        try {
            ShopsResponseDTO shops = shopsDomain.getShopsSortInfo(id);
            return ResponseUtils.getSuccess(shops);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return ResponseUtils.error(throwable.getMessage(), SystemException.Type.DEFAULT.toString());
        }
    }


    //添加
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseUtils doPost(@RequestBody ShopsDTO shopsDTO, ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
            //this.errMsg(bindingResult);
            shopsDTO.setCreatedBy(this.getOperator(request));
            log.info("添加店铺接口请求参数:{}", JSON.toJSONString(shopsDTO));
            int shops = shopsDomain.createShops(shopsDTO);
            log.info("添加店铺接口返回值:{}", shops);
            return ResponseUtils.getSuccess();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return ResponseUtils.error(throwable.getMessage(), SystemException.Type.DEFAULT.toString());
        }
    }

    //修改
    @RequestMapping(value = "/{urlParam}", method = RequestMethod.PUT)
    public ResponseUtils doPut(@PathVariable String urlParam, @RequestBody @Valid ShopsDTO shopsDTO, ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
            //this.errMsg(bindingResult);
            ShopsDTO shops = ShopsJson.createTableForModifyShops(urlParam, shopsDTO);
            shops.setUpdatedBy(this.getOperator(request));
            log.info("修改店铺接口请求参数:{}", JSON.toJSONString(shops));
            int i = shopsDomain.modifyShops(shops);
            log.info("修改店铺接口返回值:{}", i);
            return ResponseUtils.getSuccess();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return ResponseUtils.error(throwable.getMessage(), SystemException.Type.DEFAULT.toString());
        }
    }

    //删除
    @RequestMapping(value = "/{urlParam}", method = RequestMethod.DELETE)
    public String doDelete(@PathVariable String urlParam ,ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
            ShopsQuery shopsQuery = ShopsJson.createQueryForShops(urlParam);
            long id = shopsQuery.getId();
            shopsDomain.delShops(id);
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }


    @RequestMapping(value = "/lists/{urlParam:.+}", method = RequestMethod.GET)
    public String doGetLists(@PathVariable String urlParam) {
        System.out.println(urlParam);
        returnUtil = new ReturnUtil();
        try {
            ShopsQuery shopsQuery = new ShopsQuery();

            shopsQuery = JSON.parseObject(urlParam, ShopsQuery.class);

            List<ShopsTable> shopsList = shopsDomain.getShopsList(shopsQuery);

            return returnUtil.getSuccess(ShopsJson.toTableJson(shopsList));
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    /**
     * 根据店铺ID和状态查询店铺列表列表
     *  api/op/shops/getShopsList?sign=1&statusList=1,2
     */

    @GetMapping(value = "/getShopsList")
    public ResponseUtils doGetShopsList(ShopsQuery shopsQuery) {
        returnUtil = new ReturnUtil();
        try {
            List<ShopsTable> shops = shopsDomain.doGetShopsList(shopsQuery);
            return ResponseUtils.getSuccess(shops);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return ResponseUtils.error(throwable.getMessage(), SystemException.Type.DEFAULT.toString());
        }
    }





}
